package com.cdream.basic.algorithm.leetcode.findElement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cdream
 * @date 5/22/21
 */
public class Question350 {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> num1Map = new HashMap<>();
        List<Integer> res = new ArrayList<>();
        for (int i1 : nums1) {
            num1Map.put(i1, num1Map.getOrDefault(i1, 0) + 1);
        }
        for (int i2 : nums2) {
            if (num1Map.containsKey(i2) && num1Map.get(i2) > 0) {
                res.add(i2);
                num1Map.put(i2, num1Map.get(i2) -1);
            }
        }
        int[] resArray = new int[res.size()];
        int count = 0;
        for (Integer re : res) {
            resArray[count++] = re;
        }
        return resArray;


    }
}
